<!--车辆终身质保审核 经销商端-->
<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" labelWidth="100" :collapse="true" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      :onSyncTableData="tableChange"
      :isExportExcel="false"
      exportFileName="车辆终身质保审核.xlsx"
      :onCalcExportData="handleExportFun"
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button type="primary" class="btnStyle" :click="addHandle.bind(this, 1)">新增</multiuse-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <add :warrantyId="warrantyId" @close="closeHandler" :handleType="handleType" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import add from './add';
import { getAllDealer } from '@/api/repair/repairManage/repairQuery/index.js';
import { getAppId } from '@/assets/js/auth';
import { isEmpty } from '@/utils/utils';
import { vehicleWarrantyList } from '@/api/repair/newEnergyVehicleLifetime/vehicleLifetimeReviewJingxsDuan';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';

export default {
  name: 'index',
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '审核' };
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      fetchApi: vehicleWarrantyList,
      dialogTitle: '',
      fetchParams: { xhrAbort: false },
      columns: this.createTableColumns(),
      handleType: '',
      warrantyId: ''
    };
  },
  components: {
    add
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    if (getAppId() == 'arcfox') {
      this.columns = this.createTableColumnsArcfox();
      //翻译经销商
      this.getAllDealer();
    } else {
      this.columns = this.createTableColumns();
    }
    // this.getAllUsers(this.columns, ['updatedBy'], 'table');
  },
  methods: {
    dealerTranslate() {
      let userIds = [];
      this.tableData.forEach(item => {
        userIds.push(item.updatedBy);
      });
      if (!userIds.length) return;
      let newUserIds = Array.from(new Set(userIds)); // 数组去重
      this.getNameByIds(this.columns, newUserIds.join(','), ['updatedBy'], 'table');
    },
    tableChange(data) {
      this.tableData = data;
      this.dealerTranslate();
    },
    addHandle() {
      this.handleType = 'add';
      this.warrantyId = '';
      this.showHandler();
    },

    // 导出处理函数
    handleExportFun(row) {},
    // 获取所有的经销商
    async getAllDealer() {
      let res = await getAllDealer();
      if (res.resultCode == 200) {
        let allDealer = res.data.map(x => ({ text: x.dealerShortname, value: x.dealerCode + '' }));
        this.columns.find(x => x.dataIndex == 'ownerCode').dictItems = allDealer;
        // this.columns.find(x => x.dataIndex === 'ownerCode').dictItems = allDealer;
      }
    },
    detailHandle(row) {
      this.warrantyId = row['warrantyId'];
      this.handleType = 'edit';
      this.showHandler();
    },
    createTableColumnsArcfox() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          fixed: 'left',
          sorter: true,
          width: 70,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          render: props => {
            let row = props.row;
            let status = isEmpty(row['status']) ? '' : row['status'];
            let rst = false;
            if (status == '10221002') {
              rst = true;
            }
            return (
              <div>
                <multiuse-button
                  type="text"
                  size="mini"
                  // disabled={rst}
                  click={() => this.detailHandle(props.row)}
                >
                  明细
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '申请状态',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1022),
          render: props => {
            return <span>{this.createDictText(props.row.status, 1022)}</span>;
          }
        },

        {
          title: '车主姓名',
          dataIndex: 'ownerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '保修起始日期',
          dataIndex: 'salesDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '行驶证注册日期',
          dataIndex: 'driveLicenseDate',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '申请日期',
          dataIndex: 'updatedAt',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '申请人',
          dataIndex: 'updatedBy',
          sorter: true,
          dictItems: []
        },
        {
          title: '是否首任车主',
          dataIndex: 'isFirstOwner',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '是否有效',
          dataIndex: 'valid',
          sorter: true,
          dictItems: this.createDictList(1004)
        },
        {
          title: '失效原因',
          dataIndex: 'validReason',
          sorter: true
        },
        {
          title: '申请经销商',
          dataIndex: 'ownerCode',
          dictItems: [],
          sorter: true
        },
        {
          title: '质保类型',
          dataIndex: 'warrantyType',
          dictItems: this.createDictList('8888'),
          // dictItems: this.crea,
          sorter: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          fixed: 'left',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: props => {
            let row = props.row;

            let status = isEmpty(row['status']) ? '' : row['status'];

            let rst = false;
            if (status == '10221001') {
              rst = true;
            }

            return (
              <div>
                <el-button size="mini" type="text" disabled={rst} onClick={() => this.detailHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '申请状态',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1022),
          render: props => {
            return <span>{this.createDictText(props.row.status, 1022)}</span>;
          }
        },

        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车主姓名',
          dataIndex: 'ownerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '保修起始日期',
          dataIndex: 'salesDate',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '行驶证注册日期',
          dataIndex: 'driveLicenseDate',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '申请日期',
          dataIndex: 'updatedAt',
          sorter: true,
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '申请人',
          dataIndex: 'updatedBy',
          sorter: true,
          dictItems: []
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: 'VIN...'
        },
        {
          type: 'INPUT',
          label: '车主姓名',
          fieldName: 'ownerName',
          placeholder: '车主姓名...'
        },
        {
          type: 'RANGE_DATE',
          label: '行驶证注册日期',
          dateType: 'daterange',
          fieldName: 'bDriveLicenseDate|eDriveLicenseDate'
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          dateType: 'daterange',
          fieldName: 'bUpdatedAt|eUpdatedAt'
        }
      ];
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    changeHandle(val) {
      // val.bDate=moment(val['bDate']).format('YYYY-MM-DD')
      // val.eDate=moment(val['eDate']).format('YYYY-MM-DD')
      this.fetchParams = Object.assign({}, val);

      console.log(this.fetchParams);
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}车辆终身质保审核`;

      this.visible = true;
    },

    closeHandler(type) {
      this.visible = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
